<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/09/20 22:11:20 $)
// $RCSfile: staff_preferences.php,v $ : $Revision: 1.7 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_staff.php");
$template->loadLanguageSection("staff_dashboard");

$interface->assignQuickLink($_SWIFT["language"]["dashboard"], "index.php?_m=core&_a=dashboard");
$interface->assignQuickLink($_SWIFT["language"]["preferences"], "index.php?_m=core&_a=preferences");
$interface->assignQuickLink($_SWIFT["language"]["privatemessages"], "index.php?_m=core&_a=privatemessages");

/**
* ###############################################
* PREFERENCES
* ###############################################
*/
if ($eventaction == "preferences")
{
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["fullname"]) == "" || trim($_POST["email"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateStaffPreferences($_SWIFT["staff"]["staffid"], $_POST["fullname"], "", $_POST["email"], $_POST["mobilenumber"], $_POST["signature"], $_POST["timezoneoffset"], iif($_POST["enablepmalerts"]==1,true,false), iif($_POST["enablepmjsalerts"]==1,true,false), iif($_POST["enabledst"]==1, true, false));

			printRedirect($_SWIFT["language"]["informationupdated"], "index.php?_m=core&_a=preferences&infoupdated=1");
			exit;
		}
	}

	if ($_REQUEST["infoupdated"] == 1)
	{
		$infomessage = $_SWIFT["language"]["informationupdated"];
	}

	$_signature = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."signatures` WHERE `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."';");
	$interface->staffHeader($_SWIFT["language"]["dashboard"]." &gt ".$_SWIFT["language"]["preferences"], 1);
	$interface->staffNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=preferences" title="'.$_SWIFT["language"]["preferences"].'">'.$_SWIFT["language"]["preferences"].'</a>', "", 1);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart("staffform");
	printMainTableHeader($_SWIFT["language"]["staffdetails"]);

	// ======= REQUIRED FIELDS =======
	printDescRow($_SWIFT["language"]["requiredfields"]);
	printDefaultRow('<span class="tabletitle">'. $_SWIFT["language"]["staffgroup"] .'</span>', '<span class="smalltext">'. $_SWIFT["staff"]["grouptitle"] .'</span>');

	printTextRow("fullname", $_SWIFT["language"]["stafffullname"], $_SWIFT["language"]["desc_stafffullname"], "text", $_SWIFT["staff"]["fullname"]);
	printTextRow("email", $_SWIFT["language"]["staffemail"], $_SWIFT["language"]["desc_staffemail"], "text", $_SWIFT["staff"]["email"]);

	$timelist = array('99', '-12', '-11', '-10', '-9', '-8', '-7', '-6', '-5', '-4', '-3.5', '-3', '-2', '-1', '0', '1', '2', '3', '3.5', '4', '4.5', '4.5', '5', '5.5', '6', '7', '8', '9', '9.5', '10', '11', '12');
	$options = array();
	$cnt = 0;
	foreach ($timelist as $key=>$val)
	{
		$options[$cnt]["title"] = $_SWIFT["language"]["time_".$val];
		$options[$cnt]["value"] = $val;
		if ($_SWIFT["staff"]["timezoneoffset"] == $val)
		{
			$options[$cnt]["selected"] = true;
		}
		$cnt++;
	}

	printSelectRow("timezoneoffset", $_SWIFT["language"]["timezoneoffset"], $_SWIFT["language"]["desc_timezoneoffset"], $options);

	printYesNoRow("enabledst", $_SWIFT["language"]["enabledst"], $_SWIFT["language"]["desc_enabledst"], iif($_SWIFT["staff"]["enabledst"]==1, true, false));

	// ======= SETTINGS =======
	printDescRow($_SWIFT["language"]["generalsettings"]);
	printYesNoRow("enablepmalerts", $_SWIFT["language"]["enablepmalerts"], $_SWIFT["language"]["desc_enablepmalerts"], iif($_SWIFT["staff"]["enablepmalerts"]==1, true, false));
	printYesNoRow("enablepmjsalerts", $_SWIFT["language"]["enablepmjsalerts"], $_SWIFT["language"]["desc_enablepmjsalerts"], iif($_SWIFT["staff"]["enablepmjsalerts"]==1, true, false));
	// ======= OPTIONAL FIELDS =======
	printDescRow($_SWIFT["language"]["optionalfields"]);
	printTextRow("mobilenumber", $_SWIFT["language"]["staffmobilenumber"], $_SWIFT["language"]["desc_staffmobilenumber"], "text", $_SWIFT["staff"]["mobilenumber"], "20");
	printTextareaRow("signature", $_SWIFT["language"]["staffsignature"], $_SWIFT["language"]["desc_staffsignature"], $_signature["signature"], "60", "4");

	printSubmitRow($_SWIFT["language"]["updatedetails"]);

	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "preferences");
	addHiddenField("step", "1");
	printFormEnd();

	$template->assign("backurl", "index.php?_m=core&_a=dashboard");

	$interface->staffFooter();








/**
* ###############################################
* WORK SCHEDULE
* ###############################################
*/
} else if ($eventaction == "workschedule") {
	require_once ("./modules/core/functions_workschedule.php");

	$_schedule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staffschedules` WHERE `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."';");
	if (empty($_schedule["staffid"]))
	{
		trigger_error($_SWIFT["language"]["invalidschedule"], E_USER_ERROR);
	}

	$_days = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday");

	// Update??
	if ($_POST["step"] == 1)
	{
		$_args = array();
		foreach ($_days as $key=>$val)
		{
			$dayopen = $_POST[$val."_open"];
			$dayclose = $_POST[$val."_close"];
			$isenabled = iif($_POST[$val."_enabled"] == 1, true, false);

			$hourdiff = checkHourRange($dayopen, $dayclose);
			if ($hourdiff == -1 && $isenabled)
			{
				$dateerror = true;
			} else {
				$_args[$val."_enabled"] = iif($_POST[$val."_enabled"] == 1 && $hourdiff != 0, "1", "0");
				$_args[$val."_open"] = $dayopen;
				$_args[$val."_close"] = iif($_args[$val."_enabled"]=="0", "00:00", $dayclose);
			}
		}

		if ($dateerror)
		{
			$errormessage = $_SWIFT["language"]["hourrangeerror"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateStaffSchedule($_SWIFT["staff"]["staffid"], $_args);

			printRedirect($_SWIFT["language"]["workscheduleconfirm"], "index.php?_m=core&_a=workschedule&confirm=1");

			exit;
		}
	}

	if ($_GET["confirm"] == 1)
	{
		$infomessage = $_SWIFT["language"]["workscheduleconfirm"];
	}

	$_POST = array_merge($_POST, $_schedule);

	$interface->staffHeader($_SWIFT["language"]["dashboard"]." &gt ".$_SWIFT["language"]["workschedule"], 1);
	$interface->staffNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=preferences" title="'.$_SWIFT["language"]["preferences"].'">'.$_SWIFT["language"]["preferences"].'</a> &raquo; <a href="index.php?_m=core&_a=workschedule" title="'.$_SWIFT["language"]["workschedule"].'">'.$_SWIFT["language"]["workschedule"].'</a>', "", 1);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["workschedule"]);

	foreach ($_days as $key=>$val)
	{
		unset($enablecheck);
		unset($closedcheck);

		$houroption_open = buildHourSelectOptions(false, $val."_open");
		$houroption_close = buildHourSelectOptions(true, $val."_close");

		if ($_POST[$val."_enabled"] == "1")
		{
			$enabledcheck = " checked";
		} else {
			$closedcheck = " checked";
		}

		printDefaultDescRow($_SWIFT["language"][$val], '', '<input type="radio" name="'. $val .'_enabled" value="1"'. $enabledcheck .'><select name="'. $val .'_open" class="swiftselect">'. $houroption_open .'</select> =&gt; <select name="'. $val .'_close" class="swiftselect">'. $houroption_close .'</select><br/><input type="radio" name="'. $val .'_enabled" value="0"'. $closedcheck .'/>&nbsp;'.$_SWIFT["language"]["closed"]);
	}

	printSubmitRow($_SWIFT["language"]["update"]);

	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "workschedule");
	addHiddenField("step", "1");
	printFormEnd();

	$template->assign("backurl", "index.php?_m=core&_a=dashboard");

	$interface->staffFooter();




/**
* ###############################################
* CHANGE PASSWORD
* ###############################################
*/
} else if ($eventaction == "changepassword") {

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["password"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else if (trim($_POST["password"]) != "" && $_POST["password"] != $_POST["passwordconfirm"]) {
			$errormessage = $_SWIFT["language"]["passworddontmatch"];
			$_POST["password"] = "";
			$_POST["passwordconfirm"] = "";
		} else {
			changeStaffPassword($_SWIFT["staff"]["staffid"], $_POST["password"]);

			printRedirect($_SWIFT["language"]["informationupdated"], "index.php?_m=core&_a=changepassword&infoupdated=1");
			exit;
		}
	}

	if ($_REQUEST["infoupdated"] == 1)
	{
		$infomessage = $_SWIFT["language"]["informationupdated"];
	}

	$interface->staffHeader($_SWIFT["language"]["dashboard"]." &gt ".$_SWIFT["language"]["changepassword"], 1);
	$interface->staffNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=changepassword" title="'.$_SWIFT["language"]["changepassword"].'">'.$_SWIFT["language"]["changepassword"].'</a>', "", 1);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart("staffform");
	printMainTableHeader($_SWIFT["language"]["changepassword"]);

		// ======= CHANGE PASSWORD =======
	printTextRow("password", $_SWIFT["language"]["staffpassword"], $_SWIFT["language"]["desc_staffpassword"], "password", $_POST["password"], "20");
	printTextRow("passwordconfirm", $_SWIFT["language"]["staffpasswordconfirm"], $_SWIFT["language"]["desc_staffpasswordconfirm"], "password", $_POST["passwordconfirm"], "20");

	printSubmitRow($_SWIFT["language"]["updatedetails"]);
	printMainTableFooter();

	addHiddenField("_m", "core");
	addHiddenField("_a", "changepassword");
	addHiddenField("step", "1");
	printFormEnd();

	$template->assign("backurl", "index.php?_m=core&_a=dashboard");

	$interface->staffFooter();


}


?>